public class TestHeap {
    public int [] elem;
    public int usedSize;
    public TestHeap(){
        this.elem = new int[10];
    }

    //创建一个最大堆
    public void createHeap(int [] array){
        for (int i = 0; i < array.length; i++) {
            elem[i] = array[i];
            usedSize++;
        }
        //把原始数据给elem
        for (int parent = (usedSize-1-1)/2; parent >= 0 ;parent--){
            shiftDown(parent,usedSize);

        }

    }
    private void shiftDown(int parent,int len){
        int child = 2 * parent+1;
        while(child < len){
            if(child + 1 < len && elem[child] < elem[child + 1]){
                child = child + 1;
            }
            if(elem[child] > elem[parent]){
                int tmp = elem[parent];
                elem[child] = elem[parent];
                elem[parent] = tmp;
                parent = child;
                child = 2 * parent + 1;

            }
        }
    }
}
